package com.atguigu.dga230301.governance.assess;

import com.atguigu.dga230301.governance.bean.AssessParam;
import com.atguigu.dga230301.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga230301.governance.bean.GovernanceMetric;
import com.atguigu.dga230301.meta.bean.TableMetaInfo;
import org.apache.hadoop.hive.ql.parse.SemanticException;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

public abstract class Assessor {
    protected abstract void assessor(GovernanceAssessDetail governanceAssessDetail,AssessParam assessParam) throws ParseException, org.apache.hadoop.hive.ql.parse.ParseException, SemanticException;

    public final GovernanceAssessDetail mainAssess(AssessParam assessParam) {
        String assessDate = assessParam.getAssessDate();
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        //1.给考评明细做初始化
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        governanceAssessDetail.setAssessDate(assessDate);
        governanceAssessDetail.setTableName(tableMetaInfo.getTableName());
        governanceAssessDetail.setSchemaName(tableMetaInfo.getSchemaName());
        governanceAssessDetail.setMetricId(governanceMetric.getId()+"");
        governanceAssessDetail.setMetricName(governanceMetric.getMetricName());
        governanceAssessDetail.setGovernanceType(governanceMetric.getGovernanceType());
        governanceAssessDetail.setTecOwner(tableMetaInfo.getTableMetaInfoExtra().getTecOwnerUserName());
        //默认满分十分
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);
        try{
            assessor(governanceAssessDetail,assessParam);
        }catch (Exception e){
            //有异常
            governanceAssessDetail.setIsAssessException("1");
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            //将异常信息写入表中
            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0,Math.min(stringWriter.toString().length(),2000)));
        }

        governanceAssessDetail.setCreateTime(new Date());
        return governanceAssessDetail;
    }
}
